//
//  MyTaskViewController.swift
//  willdo
//
//  Created by zz on 3/14/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit

class MyTaskViewController: UIViewController, MyTaskViewDelegate, UISearchBarDelegate, UISearchResultsUpdating {

    var searchVC:UISearchController!
    
    var myTaskView:MyTaskView?
    var modle:[MyTaskModle]?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.automaticallyAdjustsScrollViewInsets = false
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        
        modle = TASK_MODLE
        myTaskView = MyTaskView(frame: view.frame, modles: modle!)
        myTaskView?.delegate = self
        view.addSubview(myTaskView!)
        myTaskView?.snp.makeConstraints({ (make) in
            make.edges.equalToSuperview()
        })
        //setupSearchBar()
    }

    func myTaskCellDidSelect(index: IndexPath) {
        let detailVC = DetailTaskViewController()
        let modle = MyTaskDetailModle(taskModle: self.modle![index.section], emergencyState: EmergencyState.emergency, leader_avatar: "", receive_avatar: "",name: "张三", commitTime: "2018.1", commitMsg: "帆帆，下个月就是十佳大学生评选了，组织部这边要先行动起来，咱们这次的场地选在了学活剧院，需要预留50位嘉宾领导的位子，其余作为观众位，至于工作人员和候选人员的作为安排，你们自己考虑下。12月25日之前务必提交一版作为方案，用excel就可以了。", file: MY_FILE[index.section], subTask: nil, taskMember: nil)
        detailVC.modle = modle
        
        detailVC.title = "任务详情"
        detailVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    func setupSearchBar(){
        searchVC = UISearchController(searchResultsController: nil)
        searchVC.searchResultsUpdater = self
        searchVC.searchBar.sizeToFit()
        searchVC.searchBar.delegate = self
        searchVC.dimsBackgroundDuringPresentation = false
        searchVC.hidesNavigationBarDuringPresentation = false
        self.definesPresentationContext = true
        self.myTaskView?.tableView.tableHeaderView = searchVC.searchBar
    }
    
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        print(searchBar.frame)
    }
    
    
    func updateSearchResults(for searchController: UISearchController) {
        let tap = UITapGestureRecognizer(target: self, action: #selector(hideKeybord))
        tap.cancelsTouchesInView = false
        self.view.addGestureRecognizer(tap)
        guard let searchText = searchController.searchBar.text else {
            myTaskView?.modles = TASK_MODLE
            return
        }
        if searchText == "" {
            myTaskView?.modles = TASK_MODLE
        }else{
            myTaskView?.modles = (myTaskView?.modles)?.filter({ (date) -> Bool in
                for charater in searchText.characters{
                    if !date.name.lowercased().contains(String(charater).lowercased()){
                        return false
                    }
                }
                return true
                //                    return (date.rangeOfString(searchString, options: NSStringCompareOptions.CaseInsensitiveSearch).location) != NSNotFound
            })
        }
        myTaskView?.tableView.reloadData()
    }

    @objc func hideKeybord(){
        self.searchVC.searchBar.resignFirstResponder()
    }
    
}
